﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class isAndas
    {
        public void isasTest()
        { 
            //is用于检查对象是否与给定类型兼容
            AAA a = new AAA();
            if (a is AAA)            
                Console.WriteLine("a is AAA");            
            else
                Console.WriteLine("a isn't AAA");

            BBB b = new BBB();
            //BBB是AAA的子类则b也判为兼容于AAA类。
            string result = b is AAA ? "b is AAA" : "b isn't AAA";
            Console.WriteLine(result);

            //as用于在兼容的类型之间进行转换 无法转换返回null而不是报异常
            //只能用于引用类型或者可以为null的类型上(xxx as int不能通过编译)
            //性能要比用try/catch捕获转换时的异常要好很多
            CCC c = new CCC();
            //a = c as AAA; //编译不通过
            a = b as AAA;

            /*
             结构体和类的区别：
             * 结构体是值类型的，类是引用类型的
             * 结构体不能被继承，类可以
             * 结构体没有构造和析构函数，类有。
            */ 
            abc a11 = new abc();
            a11.a1 = 123;
            a11.b2="awef";
            a11.a123();
            
            Console.WriteLine(a11.GetType());
            Console.WriteLine(typeof(abc));
        }
    }

    class AAA{}

    class BBB:AAA{}

    class CCC { }

    struct abc
    {
        public int a1;
        public string b2;
        public void a123()
        {
            Console.WriteLine("aaaa");
        }
    }
    
}
